//
//  RootTabBarViewController.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/27.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "RootTabBarViewController.h"
#import "DefaultViewController.h"
#import "FindViewController.h"
#import "MeViewController.h"
#import "MessageListViewController.h"
#import "AddViewController.h"


@interface RootTabBarViewController ()

@end

@implementation RootTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    NSLog(@"token=%@", token);
    
    
    // Do any additional setup after loading the view.
    DefaultViewController *defaultVC = [[DefaultViewController alloc] init];
    FindViewController *findVC = [[FindViewController alloc] init];
    MessageListViewController *messageVC = [[MessageListViewController alloc] init];
    MeViewController *meVC = [[MeViewController alloc] init];

   
    AddViewController *addVC = [[AddViewController alloc]init];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(kWidth / 2 - 35, kHeight - 50, 70, 50);
    [button setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    // 调用自定义方法 设置根视图控制器
    self.viewControllers = [self setupNavgationController:@[defaultVC, messageVC, addVC,findVC, meVC]];
    NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"accessToken"];
    if (accessToken) {
        self.selectedIndex = 0;
    }else {
        self.selectedIndex = 3;
    }
    
}

- (void)buttonAction:(UIButton *)button{
    AddViewController *addVC = [[AddViewController alloc]init];
    [self presentViewController:addVC animated:YES completion:nil];
    
    
}
// 循环创建4个页面
- (NSMutableArray *)setupNavgationController:(NSArray *)viewControllers {
    NSMutableArray *navArray = [NSMutableArray array];
    NSArray *imageNameArray = @[@"default", @"message", @"",@"find", @"me"];
    NSArray *titleArray = @[@"首页", @"消息", @"", @"发现", @"我"];
    for (int i = 0; i < 5; i++) {
        // 导航控制器
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: viewControllers[i]];
        
        NSString *imageName = [NSString stringWithFormat:@"%@.png", imageNameArray[i]];
        UIImage *image = [UIImage imageNamed:imageName];
        //image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
//        NSString *selectImageName = [NSString stringWithFormat:@"%@%d.png", imageNameArray[i], 1];
//        UIImage *selectedImage =[UIImage imageNamed:selectImageName];
//        selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        // 初始化一个tabBarItem
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:titleArray[i] image:image tag:100];
        
        [navArray addObject:nav];
        
    }
    return navArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
